<?php

class Comments_Model_DbTable_Comments extends Zend_Db_Table_Abstract
{
    protected $_name = 'comments';

    public function getComments($postuserid)
    {		
    	$row = $this->fetchall("postuserid = $postuserid AND postparent = 0 AND status=1");
    	if (!$row) {
    		throw new Exception("Could not find row ");
    	}
		
		$rowvalue = $row->toArray();
		$value=array();
		$value['row']= $rowvalue;
		foreach ($rowvalue AS $k => $row)
		{
			$rowparent = $this->fetchall('status=1 AND postparent ='.$row['ID']);			
			$value['rowparent'][$k] = $rowparent->toArray();
		}
    	return $value;
    }
	
	 public function getByCommentView($id)
    {		
    	$id = (int)$id;
    	$row = $this->fetchRow('id = ' . $id);
    	if (!$row) {
    		throw new Exception("Could not find row $id");
    	}
    	return $row->toArray();
    }
	
	public function addComments($content, $postdate, $postuserid, $postparent, $status)
    {
		$data = array(
    			'comment_content' => $content,
    			'comment_postdate' => $postdate,
    			'comment_author' => $postuserid,
    			'post_parent' => $postparent,
    			'comment_status' => $status,    						
    	);
			
		return	$this->insert($data);
    }
	
	public function changeComments($id, $status)
    {
    	$data = array(
    			'status' => $status, 						
    	);
		
    	$this->update($data, 'ID = '. (int)$id);	
    }
	
	public function updateComments($id, $content, $postdate, $postuserid, $postparent, $status)
    {
    	$data = array(
    			'content' => $content,
    			'postdate' => $postdate,
    			'postuserid' => $postuserid,
    			'postparent' => $postparent,
    			'status' => $status,    						
    	);
		
    $this->update($data, 'ID = '. (int)$id);
    }
	
     public function deletebypost($id)
    {
		$db = Zend_Db_Table::getDefaultAdapter(); 
		$str = "DELETE FROM comments WHERE `post_parent` = $id ";
		$stmt = $db->query($str);
    	//$this->delete('ID =' . (int)$id);
		return 1;
    }
	
	  public function deleteComments($id)
    {
		$db = Zend_Db_Table::getDefaultAdapter(); 
		$str = "DELETE FROM comments WHERE `ID` = $id ";
		$stmt = $db->query($str);
    	//$this->delete('ID =' . (int)$id);
		return 1;
    }
	

}

